package com.xiaoxie.web.system;

import com.xiaoxie.common.union.R;
import com.xiaoxie.system.domain.DictValue;
import com.xiaoxie.system.service.DictValueService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/system/dict/value")
@Tag(name = "数据字典项管理",description = "数据字典项管理")
public class DictValueController {

    @Resource
    private DictValueService dictValueService;

    @Operation(summary = "根据字典id查询字典项对应的值")
    @GetMapping("/{id}")
    public R getValuesById(@PathVariable("id") Integer id) {
        DictValue dictValue = dictValueService.selectDictValueById(id);
        return R.ok(dictValue);
    }

    @Operation(summary = "根据字典id查询字典项对应的值列表")
    @GetMapping("/type/{typeId}")
    public R getValuesByTypeId(@PathVariable("typeId") Integer typeId) {
        List<DictValue> dictValues = dictValueService.selectDictValueByTypeId(typeId);
        return R.ok(dictValues);
    }

    @Operation(summary = "添加数据字典项对应值")
    @PostMapping("/add")
    public R addDictValue(@RequestBody DictValue dictValue) {
        dictValueService.addDictValue(dictValue);
        return R.ok("添加成功");
    }

    @Operation(summary = "更新数据字典项对应的值")
    @PutMapping("/update")
    public R updateDictValue(@RequestBody DictValue dictValue) {
        dictValueService.updateDictValue(dictValue);
        return R.ok("修改成功");
    }

    @Operation(summary = "删除数据字典项对应的值")
    @DeleteMapping("/delete/{id}")
    public R deleteDictValue(@PathVariable("id") Integer id) {
        dictValueService.deleteDictValueById(id);
        return R.ok("删除成功");
    }

    @Operation(summary = "批量删除数据字典项对应的值")
    @DeleteMapping("/batchDelete")
    public R batchDeleteDictValue(@RequestParam("ids") String ids) {
        List<Integer> idArray = Arrays.stream(ids.split(","))
                                .map(Integer::parseInt)
                                .toList();
        dictValueService.batchDeleteDictValue(idArray);
        return R.ok("删除成功");
    }
}
